package c.b.a.b.n;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import b.f.l.s;
import c.b.a.b.b;
import c.b.a.b.c0.g;
import c.b.a.b.i;
import c.b.a.b.j;
import c.b.a.b.k;
import c.b.a.b.l;
import c.b.a.b.z.c;
import c.b.a.b.z.d;
import com.google.android.material.internal.f;
import java.lang.ref.WeakReference;

public class a extends Drawable implements f.b {
    private static final int r = k.Widget_MaterialComponents_Badge;
    private static final int s = b.badgeStyle;

    /* renamed from: b  reason: collision with root package name */
    private final WeakReference<Context> f1895b;

    /* renamed from: c  reason: collision with root package name */
    private final g f1896c = new g();

    /* renamed from: d  reason: collision with root package name */
    private final f f1897d;
    private final Rect e = new Rect();
    private final float f;
    private final float g;
    private final float h;
    private final C0087a i;
    private float j;
    private float k;
    private int l;
    private float m;
    private float n;
    private float o;
    private WeakReference<View> p;
    private WeakReference<ViewGroup> q;

    /* renamed from: c.b.a.b.n.a$a  reason: collision with other inner class name */
    public static final class C0087a implements Parcelable {
        public static final Parcelable.Creator<C0087a> CREATOR = new C0088a();
        /* access modifiers changed from: private */

        /* renamed from: b  reason: collision with root package name */
        public int f1898b;
        /* access modifiers changed from: private */

        /* renamed from: c  reason: collision with root package name */
        public int f1899c;
        /* access modifiers changed from: private */

        /* renamed from: d  reason: collision with root package name */
        public int f1900d = 255;
        /* access modifiers changed from: private */
        public int e = -1;
        /* access modifiers changed from: private */
        public int f;
        /* access modifiers changed from: private */
        public CharSequence g;
        /* access modifiers changed from: private */
        public int h;
        /* access modifiers changed from: private */
        public int i;

        /* renamed from: c.b.a.b.n.a$a$a  reason: collision with other inner class name */
        static class C0088a implements Parcelable.Creator<C0087a> {
            C0088a() {
            }

            /* renamed from: a */
            public C0087a createFromParcel(Parcel parcel) {
                return new C0087a(parcel);
            }

            /* renamed from: b */
            public C0087a[] newArray(int i) {
                return new C0087a[i];
            }
        }

        public C0087a(Context context) {
            this.f1899c = new d(context, k.TextAppearance_MaterialComponents_Badge).f1923b.getDefaultColor();
            this.g = context.getString(j.mtrl_badge_numberless_content_description);
            this.h = i.mtrl_badge_content_description;
        }

        protected C0087a(Parcel parcel) {
            this.f1898b = parcel.readInt();
            this.f1899c = parcel.readInt();
            this.f1900d = parcel.readInt();
            this.e = parcel.readInt();
            this.f = parcel.readInt();
            this.g = parcel.readString();
            this.h = parcel.readInt();
            this.i = parcel.readInt();
        }

        public int describeContents() {
            return 0;
        }

        public void writeToParcel(Parcel parcel, int i2) {
            parcel.writeInt(this.f1898b);
            parcel.writeInt(this.f1899c);
            parcel.writeInt(this.f1900d);
            parcel.writeInt(this.e);
            parcel.writeInt(this.f);
            parcel.writeString(this.g.toString());
            parcel.writeInt(this.h);
            parcel.writeInt(this.i);
        }
    }

    private a(Context context) {
        this.f1895b = new WeakReference<>(context);
        com.google.android.material.internal.g.c(context);
        Resources resources = context.getResources();
        this.f = (float) resources.getDimensionPixelSize(c.b.a.b.d.mtrl_badge_radius);
        this.h = (float) resources.getDimensionPixelSize(c.b.a.b.d.mtrl_badge_long_text_horizontal_padding);
        this.g = (float) resources.getDimensionPixelSize(c.b.a.b.d.mtrl_badge_with_text_radius);
        f fVar = new f(this);
        this.f1897d = fVar;
        fVar.e().setTextAlign(Paint.Align.CENTER);
        this.i = new C0087a(context);
        s(k.TextAppearance_MaterialComponents_Badge);
    }

    private void b(Context context, Rect rect, View view) {
        float f2;
        int p2 = this.i.i;
        this.k = (float) ((p2 == 8388691 || p2 == 8388693) ? rect.bottom : rect.top);
        if (i() <= 9) {
            f2 = !j() ? this.f : this.g;
            this.m = f2;
            this.o = f2;
        } else {
            float f3 = this.g;
            this.m = f3;
            this.o = f3;
            f2 = (this.f1897d.f(f()) / 2.0f) + this.h;
        }
        this.n = f2;
        int dimensionPixelSize = context.getResources().getDimensionPixelSize(j() ? c.b.a.b.d.mtrl_badge_text_horizontal_edge_offset : c.b.a.b.d.mtrl_badge_horizontal_edge_offset);
        int p3 = this.i.i;
        this.j = (p3 == 8388659 || p3 == 8388691 ? s.x(view) != 0 : s.x(view) == 0) ? (((float) rect.right) + this.n) - ((float) dimensionPixelSize) : (((float) rect.left) - this.n) + ((float) dimensionPixelSize);
    }

    public static a c(Context context) {
        return d(context, (AttributeSet) null, s, r);
    }

    private static a d(Context context, AttributeSet attributeSet, int i2, int i3) {
        a aVar = new a(context);
        aVar.k(context, attributeSet, i2, i3);
        return aVar;
    }

    private void e(Canvas canvas) {
        Rect rect = new Rect();
        String f2 = f();
        this.f1897d.e().getTextBounds(f2, 0, f2.length(), rect);
        canvas.drawText(f2, this.j, this.k + ((float) (rect.height() / 2)), this.f1897d.e());
    }

    private String f() {
        if (i() <= this.l) {
            return Integer.toString(i());
        }
        Context context = (Context) this.f1895b.get();
        if (context == null) {
            return "";
        }
        return context.getString(j.mtrl_exceed_max_badge_number_suffix, new Object[]{Integer.valueOf(this.l), "+"});
    }

    private void k(Context context, AttributeSet attributeSet, int i2, int i3) {
        TypedArray k2 = com.google.android.material.internal.g.k(context, attributeSet, l.Badge, i2, i3, new int[0]);
        p(k2.getInt(l.Badge_maxCharacterCount, 4));
        if (k2.hasValue(l.Badge_number)) {
            q(k2.getInt(l.Badge_number, 0));
        }
        m(l(context, k2, l.Badge_backgroundColor));
        if (k2.hasValue(l.Badge_badgeTextColor)) {
            o(l(context, k2, l.Badge_badgeTextColor));
        }
        n(k2.getInt(l.Badge_badgeGravity, 8388661));
        k2.recycle();
    }

    private static int l(Context context, TypedArray typedArray, int i2) {
        return c.a(context, typedArray, i2).getDefaultColor();
    }

    private void r(d dVar) {
        Context context;
        if (this.f1897d.d() != dVar && (context = (Context) this.f1895b.get()) != null) {
            this.f1897d.h(dVar, context);
            u();
        }
    }

    private void s(int i2) {
        Context context = (Context) this.f1895b.get();
        if (context != null) {
            r(new d(context, i2));
        }
    }

    private void u() {
        Context context = (Context) this.f1895b.get();
        WeakReference<View> weakReference = this.p;
        ViewGroup viewGroup = null;
        View view = weakReference != null ? (View) weakReference.get() : null;
        if (context != null && view != null) {
            Rect rect = new Rect();
            rect.set(this.e);
            Rect rect2 = new Rect();
            view.getDrawingRect(rect2);
            WeakReference<ViewGroup> weakReference2 = this.q;
            if (weakReference2 != null) {
                viewGroup = (ViewGroup) weakReference2.get();
            }
            if (viewGroup != null || b.f1901a) {
                if (viewGroup == null) {
                    viewGroup = (ViewGroup) view.getParent();
                }
                viewGroup.offsetDescendantRectToMyCoords(view, rect2);
            }
            b(context, rect2, view);
            b.d(this.e, this.j, this.k, this.n, this.o);
            this.f1896c.S(this.m);
            if (!rect.equals(this.e)) {
                this.f1896c.setBounds(this.e);
            }
        }
    }

    private void v() {
        this.l = ((int) Math.pow(10.0d, ((double) h()) - 1.0d)) - 1;
    }

    public void a() {
        invalidateSelf();
    }

    public void draw(Canvas canvas) {
        if (!getBounds().isEmpty() && getAlpha() != 0 && isVisible()) {
            this.f1896c.draw(canvas);
            if (j()) {
                e(canvas);
            }
        }
    }

    public CharSequence g() {
        Context context;
        if (!isVisible()) {
            return null;
        }
        if (!j()) {
            return this.i.g;
        }
        if (this.i.h <= 0 || (context = (Context) this.f1895b.get()) == null) {
            return null;
        }
        return context.getResources().getQuantityString(this.i.h, i(), new Object[]{Integer.valueOf(i())});
    }

    public int getAlpha() {
        return this.i.f1900d;
    }

    public int getIntrinsicHeight() {
        return this.e.height();
    }

    public int getIntrinsicWidth() {
        return this.e.width();
    }

    public int getOpacity() {
        return -3;
    }

    public int h() {
        return this.i.f;
    }

    public int i() {
        if (!j()) {
            return 0;
        }
        return this.i.e;
    }

    public boolean isStateful() {
        return false;
    }

    public boolean j() {
        return this.i.e != -1;
    }

    public void m(int i2) {
        int unused = this.i.f1898b = i2;
        ColorStateList valueOf = ColorStateList.valueOf(i2);
        if (this.f1896c.w() != valueOf) {
            this.f1896c.U(valueOf);
            invalidateSelf();
        }
    }

    public void n(int i2) {
        if (this.i.i != i2) {
            int unused = this.i.i = i2;
            WeakReference<View> weakReference = this.p;
            if (weakReference != null && weakReference.get() != null) {
                View view = (View) this.p.get();
                WeakReference<ViewGroup> weakReference2 = this.q;
                t(view, weakReference2 != null ? (ViewGroup) weakReference2.get() : null);
            }
        }
    }

    public void o(int i2) {
        int unused = this.i.f1899c = i2;
        if (this.f1897d.e().getColor() != i2) {
            this.f1897d.e().setColor(i2);
            invalidateSelf();
        }
    }

    public boolean onStateChange(int[] iArr) {
        return super.onStateChange(iArr);
    }

    public void p(int i2) {
        if (this.i.f != i2) {
            int unused = this.i.f = i2;
            v();
            this.f1897d.i(true);
            u();
            invalidateSelf();
        }
    }

    public void q(int i2) {
        int max = Math.max(0, i2);
        if (this.i.e != max) {
            int unused = this.i.e = max;
            this.f1897d.i(true);
            u();
            invalidateSelf();
        }
    }

    public void setAlpha(int i2) {
        int unused = this.i.f1900d = i2;
        this.f1897d.e().setAlpha(i2);
        invalidateSelf();
    }

    public void setColorFilter(ColorFilter colorFilter) {
    }

    public void t(View view, ViewGroup viewGroup) {
        this.p = new WeakReference<>(view);
        this.q = new WeakReference<>(viewGroup);
        u();
        invalidateSelf();
    }
}
